// Copyright (c) 2025 Zhou Yiyan
// Dynamic World Generation Library is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

// Example demonstrating similarity-based edge blending.
package main

import (
	"fmt"
	"log"

	"gitee.com/sweetiron/world-gen"
)

func main() {
	seed := int64(12345)
	generator := worldgen.NewNoiseGenerator(seed)

	// Generate first chunk
	chunk1, err := generator.GenerateChunk(worldgen.ChunkCoord{X: 0, Y: 0},
		worldgen.DefaultDifficulty, 0.0, nil) // No similarity
	if err != nil {
		log.Fatal(err)
	}

	// Generate second chunk with high similarity to first
	neighbors := map[worldgen.ChunkCoord]worldgen.ChunkData{
		{X: 0, Y: 0}: chunk1,
	}

	chunk2, err := generator.GenerateChunk(worldgen.ChunkCoord{X: 1, Y: 0},
		worldgen.DefaultDifficulty, 0.9, neighbors) // High similarity
	if err != nil {
		log.Fatal(err)
	}

	// Display both chunks for comparison
	fmt.Println("Chunk (0,0) - No similarity:")
	printChunkEdge(chunk1)

	fmt.Println("\nChunk (1,0) - High similarity (0.9) to chunk (0,0):")
	printChunkEdge(chunk2)

	fmt.Println("\nNote: Right edge of chunk (0,0) should influence left edge of chunk (1,0)")
}

// printChunkEdge prints only the edge tiles of a chunk for better visualization.
func printChunkEdge(chunk worldgen.ChunkData) {
	for y := 0; y < worldgen.ChunkSize; y++ {
		for x := 0; x < worldgen.ChunkSize; x++ {
			if x == 0 || x == worldgen.ChunkSize-1 || y == 0 || y == worldgen.ChunkSize-1 {
				fmt.Printf("%c", tileToChar(chunk[y][x]))
			} else {
				fmt.Printf(" ")
			}
		}
		fmt.Println()
	}
}

// tileToChar converts a TileType to a single ASCII character for display.
func tileToChar(tile worldgen.TileType) rune {
	switch tile {
	case worldgen.TileTypeEmpty:
		return '.'
	case worldgen.TileTypeGrass:
		return 'g'
	case worldgen.TileTypeWater:
		return 'w'
	case worldgen.TileTypeForest:
		return 'f'
	case worldgen.TileTypeMountain:
		return 'm'
	case worldgen.TileTypeDesert:
		return 'd'
	case worldgen.TileTypeLava:
		return 'l'
	default:
		return '?'
	}
}
